package com.example.coreshopapp.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.coreshopapp.R;

import java.util.ArrayList;
import java.util.List;

import bean.OrderItemBean;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {
    private List<OrderItemBean> orderItemList = new ArrayList<>();

    public OrderAdapter(List<OrderItemBean> orderItemList) {
        this.orderItemList = orderItemList;
    }

    public void updateOrderAdapter(List<OrderItemBean> orderItemList) {
        this.orderItemList = orderItemList;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public OrderAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        View inflate = View.inflate(parent.getContext(), R.layout.item_order_pay, null);
        View inflate = LayoutInflater.from(parent.getContext()).inflate( R.layout.item_order_pay, parent,false);
        return new ViewHolder(inflate);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull OrderAdapter.ViewHolder holder, int position) {
        OrderItemBean orderItem = orderItemList.get(position);
        holder.cartQuantityTv.setText("×" + orderItem.getNums());
        holder.cartSpecTv.setText(orderItem.getUnit());
        holder.cartNameTv.setText(orderItem.getName());
        holder.cartPriceTv.setText(orderItem.getAmount() + "");
        holder.priceTv.setText(orderItem.getPrice()+ "");
        holder.actualpriceTv.setText((orderItem.getPromotionPrice() > 0? orderItem.getPromotionPrice():orderItem.getPrice()) + "");
        holder.codeTv.setText(orderItem.getGoodsId() +"");
        holder.entrynoTV.setText(position + 1 +".");

    }

    @Override
    public int getItemCount() {
        return orderItemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView cartNameTv, cartQuantityTv, cartPriceTv, cartSpecTv, codeTv, priceTv, actualpriceTv,entrynoTV;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cartNameTv = itemView.findViewById(R.id.cart_name_tv);
            cartPriceTv = itemView.findViewById(R.id.cart_price_tv);
            cartQuantityTv = itemView.findViewById(R.id.cart_quantity_tv);
            cartSpecTv = itemView.findViewById(R.id.cart_spec_tv);
            codeTv = itemView.findViewById(R.id.tv_code);
            priceTv = itemView.findViewById(R.id.tv_price);
            actualpriceTv = itemView.findViewById(R.id.tv_actualprice);
            entrynoTV = itemView.findViewById(R.id.tv_entryno);
        }
    }
}
